"use strict";
/* Controllers */
app.controller("ondutyDashboardController", ['$scope', '$location', '$rootScope', '$routeParams', 'ondutyDashboardService', function ($scope, $location, $rootScope, $routeParams, ondutyDashboardService) {

    var vm = $scope.vm;
    vm.p = {};
    vm.p.interval = 'week';

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listTicketDashboard = function () {
        var vm = $scope.vm;
        var params = {};
        params = vm.p;
        $('#list_table').bootstrapTable('showLoading');
        // $('#ganji_table').bootstrapTable('showLoading');
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            $('#list_table').bootstrapTable('load', data);
            $('#list_table').bootstrapTable('hideLoading');
            // $('#ganji_table').bootstrapTable('load', data.ganji)

        };
        ondutyDashboardService.listTicketDashboard(params, procFn, errorFn);
    };

     $scope.loadGroupTrend = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            $scope.initChart('group_trend', data);
        };
        ondutyDashboardService.loadGroupTrend(params, procFn, errorFn);
     };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 100, 1000], pageSize: 30
        });

        // var $resultTable2 = $('#ganji_table');
        // $resultTable2.bootstrapTable({
        //      pageList: [30, 100,1000], pageSize: 30
        // });

        // $resultTable2.bootstrapTable('hideColumn', 'costHjzw');
        // $resultTable2.bootstrapTable('hideColumn', 'costYouxuan');
        // $resultTable2.bootstrapTable('hideColumn', 'resumeDeliveryNum');
        // $resultTable2.bootstrapTable('hideColumn', 'imNum');
        // $resultTable2.bootstrapTable('hideColumn', 'telConnNum');


        $scope.listTicketDashboard();
        $scope.loadGroupTrend();
    };

    $scope.searchOndutyDashboard = function () {
        $('#list_table').bootstrapTable('refresh');
        $scope.listTicketDashboard()
    };
    $scope.toggleInterval = function (interval) {
        interval && ($scope.vm.p.interval = interval);
        $scope.searchOndutyDashboard()
        $scope.loadGroupTrend()
    }

    $scope.initializeController();

$scope.initChart = function(id, rs) {
        var dom = document.getElementById(id);
        var myChart = echarts.init(dom);
        window.onresize = function () {
            myChart.resize();
        };
        var series = [];
        var arr = rs.values;
        for(var i=0;i<arr.length;++i) {
            var item = {
                name: arr[i].name,
                yAxisIndex: 0,
                data: arr[i].data,
                type: 'line',
                smooth: false
            };
            series.push(item);
        }

        var app = {};
        var option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: { type: 'cross' }
            },
            legend: {
            },
            dataZoom: [{}],
            // 时间序列
            xAxis: {
                type: 'category',
                data: rs.xAxisData
            },
            yAxis: [{
                    name: '指标',
                    type: 'value'
                }
            ],
            series: series
        };
        window.onresize = function () {
            myChart.resize();
        };
        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }
    }
}]);

/* Services */
app.service('ondutyDashboardService', ['ajaxService', function (ajaxService) {

    this.listTicketDashboard = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/ticket/listTicketDashboard.do", successFunction, errorFunction);
    };

    this.loadGroupTrend = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/ticket/findTicketChartData.do", successFunction, errorFunction);
    };
}]);

function missRateToPercent(value, row, index) {
    if (value == null || typeof (value) == 'undefined') {
        return '-'
    }
    return row.missCnt + '(' + value + '%)';
}
function errorReportRateToPercent(value, row, index) {
    if (value == null || typeof (value) == 'undefined') {
        return '-'
    }
    return row.errorReportCnt + '(' + value + '%)';
}

function resolvegroupIdDashFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/star/onduty/ticket_list/rg/', value, '" target="_blank">', row.resolvegroupName,'</a>'
    ].join('');
}

function ctiIdDashFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/star/onduty/ticket_list/rg/', row.resolvegroupId, '" target="_blank">', row.ctiName,'</a>'
    ].join('');
}

// function wubaWordFormat(value, row, index) {
//     return wordFormat(value, 'wuba')
// }

// // function ganjiWordFormat(value, row, index) {
// //     return wordFormat(value, 'ganji')
// // }

// function wordFormat(value, business) {
//     if (value == null) {
//         return "-";
//     }
//     //通过controller来获取Angular应用
//     var appElement = document.querySelector('[ng-controller=index]');
//     //获取$scope变量
//     var $scope = angular.element(appElement).scope();
//     $scope.vm.p.business = business;
//     $scope.vm.p.inword = value;

//     let urlParam = $scope.vm.p
//     if (urlParam.catename != '' && typeof (urlParam.catename) != "undefined" && urlParam.catename.indexOf("/") >= 0) {
//         urlParam.catename = urlParam.catename.replaceAll('/', '|')
//         console.log(urlParam)
//     }
//     // var str='<a href="#/star/main/words_chart' + value + '" target="_blank">' + value + '</a>'    
//     var str = '<a href="#/star/main/words_chart/' + encodeURIComponent(JSON.stringify(urlParam)) + '" target="_blank">' + value + '</a>';
//     return str;

// }

// /** 
//  * param 将要转为URL参数字符串x的对象 
//  * key URL参数字符串的前缀 
//  * encode true/false 是否进行URL编码,默认为true 
//  *  
//  * return URL参数字符串 
//  */
// var urlEncode = function (param, key, encode) {
//     if (param == null) return '';
//     var paramStr = '';
//     var t = typeof (param);
//     if (t == 'string' || t == 'number' || t == 'boolean') {
//         paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param);
//     } else {
//         for (var i in param) {
//             var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
//             paramStr += urlEncode(param[i], k, encode);
//         }
//     }
//     return paramStr;
// };




